package com.car.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.commons.core.model.WoniuException;
import com.car.order.entity.form.serveOrderProcess.ServiceOrderProcessFindListForm;
import com.car.order.entity.form.serveOrderProcess.ServiceOrderProcessFindPageForm;
import com.car.order.entity.form.serveOrderProcess.ServiceOrderProcessInsertForm;
import com.car.order.entity.form.serveOrderProcess.ServiceOrderProcessUpdateForm;
import com.car.order.mapper.ServiceOrderProcessMapper;
import com.car.order.entity.ServiceOrderProcess;
import com.car.order.service.ServiceOrderProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import java.util.List;

/**
 * @author 杜岷芫
 * @since 2024-08-29 11:30:53
 */
@Service
@Slf4j
public class ServiceOrderProcessServiceImpl extends ServiceImpl<ServiceOrderProcessMapper, ServiceOrderProcess> implements ServiceOrderProcessService {

    @Autowired
    private ServiceOrderProcessMapper serviceOrderProcessMapper;

    @Override
    public Page<ServiceOrderProcess> findPage(ServiceOrderProcessFindPageForm form) {
        Page<ServiceOrderProcess> page = new Page<>(form.getPageNum()==null?1:form.getPageNum(),form.getPageSize()==null?6:form.getPageSize());
        QueryWrapper<ServiceOrderProcess> query = new QueryWrapper<>();
        return serviceOrderProcessMapper.selectPage(page, query);
    }

    @Override
    public List<ServiceOrderProcess> findList(ServiceOrderProcessFindListForm form){
        QueryWrapper<ServiceOrderProcess> query = new QueryWrapper<>();
        return serviceOrderProcessMapper.selectList(query);
    }

    @Override
    public ServiceOrderProcess findById(Integer id) {
        return serviceOrderProcessMapper.selectById(id);
    }

    @Override
    public ServiceOrderProcess insert(ServiceOrderProcessInsertForm form) {
        ServiceOrderProcess serviceOrderProcess = BeanUtil.toBean(form, ServiceOrderProcess.class);
        boolean save = save(serviceOrderProcess);
        if (!save)
            throw new WoniuException(500, "新增失败");
        return serviceOrderProcess;
    }

    @Override
    public ServiceOrderProcess update(ServiceOrderProcessUpdateForm form) {
        ServiceOrderProcess serviceOrderProcess = BeanUtil.toBean(form, ServiceOrderProcess.class);
        boolean b = updateById(serviceOrderProcess);
        if (!b)
            throw new WoniuException(500, "修改失败");
        return serviceOrderProcess;
    }

    @Override
    public void delete(Integer id) {
        Integer i=serviceOrderProcessMapper.deleteById(id);
        if (i==0)
            throw new WoniuException(500, "删除失败");
    }

}